define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    var iconClassMap = require("core/utils/uploadUtil");
    $.widget('rk.colleagueContentFile', {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me.loaddata("fileList",{page:1,size:20,profileUId:opt.uid});
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='group_file_downLoad_button']", function(){
                var button = $(this);
                var tfcode = button.attr("tfcode");
                var fileId = button.attr("fileId");
                window.location.href = "/json/sns_twitter_file/download.action?fileId="+fileId+"&checkCode="+tfcode;
            });
            elem.on("click", "[act='group_file_delete_button']", function(){
                var button = $(this);
                var fileId = button.attr("fileId");
                rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_DELETE'),{okText:rk.i18n('COLLEAGUE_DELETE')}, function () {
                    rk.post("/json/sns_twitter_file/delete.action",{"fileId":fileId},function(json){
                        if(json.status === 0){
                            if(me.loaddataParam) {
                                elem.beLoading();
                                setTimeout(function(){
                                    me.loaddata(me.loaddataParam.type, me.loaddataParam.param);
                                }, 1000);
                            }
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                    });
                }, function () {
                    //取消
                });
            });
            elem.on("click", "[act='group_file_favTag_button']", function(){
                var button = $(this);
                var favStatus = button.attr("favStatus");
                var fileId = button.attr("fileId");
                if(favStatus == 1){
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_COLLECT'), function () {
                        rk.post("/json/sns_twitter_file_fav/cancel.action",{"sid":fileId},function(json){
                            if(json.status === 0){
                                button.attr("favStatus","0");
                                button.removeClass("file_collect");
                                button.addClass("file_uncollect");
                            }else{
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        });
                    }, function () {
                        //取消
                    });
                }else if(favStatus == 0){
                    if(button.hasClass('disable')) return;
                    button.addClass('disable');
                    rk.post("/json/sns_twitter_file_fav/save.action",{"sid":fileId},function(json){
                        if(json.status === 0){
                            button.removeClass("disable");
                            button.attr("favStatus","1");
                            button.removeClass("file_uncollect");
                            button.addClass("file_collect");
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                    });
                }
            });
        },
        loaddataParam:"",
        loaddata:function(type,param){
            var temp = require("oa/tmpl/colleague/content/tpl_colleague_content_files.tpl");
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.beLoading();
            var showHtml = function(json){
                if(json.data.twitterFiles && json.data.twitterFiles.length > 0){
                    var newTwitterFiles = [];
                    $.each(json.data.twitterFiles, function(i,dataJson){
                        var updateDate = new Date(dataJson.updatedAt-0);
                        var year = updateDate.getFullYear();
                        var month = updateDate.getMonth()+1;
                        var day = updateDate.getDate();
                        month = (month+"").length == 1 ? "0" + month : month;
                        day = (day+"").length == 1 ? "0" +day :day;
                        dataJson.updateDateString = year + "-" + month + "-" +day;

                        var arr = dataJson.fileName.split('.');
                        var fileType = '.' + arr[arr.length - 1];
                        dataJson.file_class = iconClassMap.getIconClass(fileType);
                        newTwitterFiles[i] = dataJson;
                    });
                    json.data.twitterFiles = newTwitterFiles;
                }
                var html = rk.templateText(temp,json.data);
                elem.html(html);
            };
            me.loaddataParam = {"type":type,param:param};
            rk.post("/json/oa_twitter-file/list-profile-files.action",param,function(json){
                if(json.status === 0){
                    if(json.data.twitterFiles && json.data.twitterFiles.length>0) {
                        showHtml(json);
                        //colleague_user_file_page
                        elem.find('[act="colleague_user_file_page"]').rkPaging({
                            pagenum: json.data.page,  //（可选）当前页数，默认1
                            pagesize: param.size,  //（可选）当前每页显示条数，默认20
                            rowscount: json.data.total, //所有数据数
                            onChange: function (pagenum) {
                                var params = param;
                                params.page = pagenum;
                                //params.size = json.pageSize;
                                me.loaddata("", params);
                            }
                        });
                    }else{
                        var no_temp = require("oa/tmpl/colleague/tpl_colleague_none.tpl");
                        var html = rk.templateText(no_temp,{text:rk.i18n('COLLEAGUE_NO_FILE')});
                        elem.html(html);
                    }
                }else{
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
            });
        }
    });
});